package com.aierkeji.call.cms.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.aierkeji.call.cms.contants.Contants;
import com.aierkeji.call.cms.service.AkjCallClueGroupService;
import com.aierkeji.call.cms.service.AkjCallClueService;
import com.aierkeji.call.cms.util.DateTimeUtils;
import com.aierkeji.call.cms.util.ResultMapHelper;
import com.aierkeji.call.cms.util.WebUtils;

@RestController
@RequestMapping("/clue")
public class AkjCallClueController extends BaseController {
	private final Logger log = LogManager.getLogger(getClass());

	@Autowired
	AkjCallClueService akjCallClueService;
	@Autowired
	AkjCallClueGroupService akjCallClueGroupService;

	@PostMapping("group/{pageNo}")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER, Contants.ROLE_ID_ADMIN }, logical = Logical.OR)
	public Map<String, Object> groupList(@PathVariable(name = "pageNo", required = true) Integer pageNo,
			@RequestBody Map<String, Object> request) {
		return ResultMapHelper.success(akjCallClueGroupService.getClueGroup(pageNo));
	}

	@PostMapping("group/create")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER }, logical = Logical.OR)
	public Map<String, Object> createCroup(@RequestBody Map<String, Object> request) {
		return akjCallClueGroupService.createCullGroup(request);
	}

	@PostMapping("group/edit")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER }, logical = Logical.OR)
	public Map<String, Object> editCroup(@RequestBody Map<String, Object> request) {
		return akjCallClueGroupService.editCullGroup(request);
	}

	@PostMapping("group/delete/{id}")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER }, logical = Logical.OR)
	public Map<String, Object> deleteCroup(@PathVariable(name = "id", required = true) Long id,
			@RequestBody Map<String, Object> request) {
		return akjCallClueGroupService.deleteCullGroup(id, request);
	}

	@PostMapping("uploadFile")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER }, logical = Logical.OR)
	public Map<String, Object> uploadFile(@RequestParam(name = "file", required = false) MultipartFile file,
			@RequestParam(name = "id", required = false) String id) throws IOException {
		return akjCallClueService.uploadFile(file, id);

	}

	@PostMapping("/query/{pageNo}")
	public Map<String, Object> query(@PathVariable(name = "pageNo", required = true) Integer pageNo,
			@RequestBody Map<String, Object> request) throws IOException {
		return ResultMapHelper.success(akjCallClueService.query(pageNo, request));

	}

	@PostMapping("/delete")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER }, logical = Logical.OR)
	public Map<String, Object> delete(@RequestBody Map<String, Object> request) throws IOException {
		return akjCallClueService.delete(request);
	}

	@PostMapping("/checkDuplicate/{groupId}")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER }, logical = Logical.OR)
	public Map<String, Object> checkDuplicate(@PathVariable(name = "groupId", required = true) Long groupId)
			throws IOException {
		return akjCallClueService.checkDuplicate(groupId);

	}

	/**
	 * 检查分配信息
	 * 
	 * @param groupId
	 * @return
	 * @throws IOException
	 */
	@PostMapping("assigne/{groupId}")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER }, logical = Logical.OR)
	public Map<String, Object> assigne(@PathVariable(name = "groupId", required = true) Long groupId)
			throws IOException {
		return akjCallClueService.assigne(groupId);

	}

	/**
	 * 分配线索
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@PostMapping("assigne")
	@RequiresRoles(value = { Contants.ROLE_ID_MANAGER }, logical = Logical.OR)
	public Map<String, Object> assigne(@RequestBody Map<String, Object> request) throws IOException {
		return akjCallClueService.assigne(request);

	}

	@PostMapping("getQueryConfig")
	public Map<String, Object> getQueryConifg() throws IOException {
		return akjCallClueService.getQueryConifg();

	}

	@PostMapping("getQueryConfig/{managerId}")
	@RequiresRoles(value = { Contants.ROLE_ID_ADMIN }, logical = Logical.OR)
	public Map<String, Object> getQueryConifg(@PathVariable("managerId") long managerId) throws IOException {
		return akjCallClueService.getQueryConifg(managerId);

	}

	@GetMapping("download")
	public ResponseEntity download(HttpServletRequest request) throws IOException {
		byte[] bytes = null;
		HttpStatus statusCode = HttpStatus.OK;
		HttpHeaders headers = new HttpHeaders();
		Map<String, Object> resultMap = null;
		try {
			resultMap = akjCallClueService.download(WebUtils.getParamsMap(request));
		} catch (Exception e) {
			log.error("导出线索列表错误:", e);
			headers.setContentType(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8"));
			ResponseEntity<String> entity = new ResponseEntity<String>("发生未知错误请联系管理员!", headers, statusCode);
			return entity;
		}
		if (ResultMapHelper.isSuccess(resultMap)) {
			bytes = resultMap.get("data").toString().getBytes("GBK");
		} else {
			headers.setContentType(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8"));
			ResponseEntity<String> entity = new ResponseEntity<String>((String) resultMap.get("msg"), headers,
					statusCode);
			return entity;
		}

		headers.setContentType(MediaType.valueOf("application/x-download"));
		headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode("线索列表", "UTF-8") + ".csv");
		ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(bytes, headers, statusCode);
		return entity;

	}

	@PostMapping("history/{id}")
	public Map<String, Object> history(@PathVariable(name = "id", required = true) Long clueId) throws IOException {
		return akjCallClueService.history(clueId);

	}

	@GetMapping("backUp")
	@RequiresRoles(value = { Contants.ROLE_ID_ADMIN }, logical = Logical.OR)
	public Map<String, Object> backUp(HttpServletRequest request) {
		String date = request.getParameter("startDate");
		Date s = DateTimeUtils.converStringToDate(date);
		akjCallClueService.assigneBackUps(s);
		return ResultMapHelper.success();
	}
}
